package com.oa.ylms.apps.business.move.impl;

import java.util.List;

import com.oa.ylms.apps.business.archives.IArchiveInfoService;
import com.oa.ylms.apps.business.archives.IRecordInfoService;
import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.move.IPersonMoveCheckService;
import com.oa.ylms.apps.business.move.IPersonMoveInfoService;
import com.oa.ylms.apps.business.organization.IPostInfoService;
import com.oa.ylms.apps.business.plan.ICheckPlanService;
import com.oa.ylms.apps.business.plan.IMessageInfoService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.ICheckStates;
import com.oa.ylms.apps.constants.IOrderStates;
import com.oa.ylms.apps.constants.IPlanCategory;
import com.oa.ylms.apps.constants.IRecordCategroyStates;
import com.oa.ylms.apps.constants.goods.IGoodsInfoStates;
import com.oa.ylms.apps.dao.move.IPersonMoveInfoDao;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.move.PersonMoveCheckValue;
import com.oa.ylms.apps.entity.move.PersonMoveInfoValue;
import com.oa.ylms.apps.entity.plan.CheckPlanValue;

public class PersonMoveInfoServiceImpl implements IPersonMoveInfoService {
	private IPostInfoService postInfoService;
	private IStaffInfoService staffInfoService;
	private ICheckPlanService checkPlanService;
	private IPersonMoveCheckService moveCheckService;
	private PersonMoveInfoValue personMoveInfo;
	private PersonMoveCheckValue personMoveCheck;
	private IPersonMoveInfoDao infoDao;
	private IArchiveInfoService archiveInfoService;
	private IRecordInfoService  recordService;
	private IMessageInfoService messageService;

	@Override
	public int addPersonMoveInfo(int postId, StaffInfoValue staffInfo, String mtoIds,String comments)
			throws Exception {
		int currentPost=staffInfo.getArchiveInfo().getPostInfo().getPostId();
		CheckPlanValue plan= checkPlanService.queryByPostIdAndCategoryId(IPlanCategory.MOVECHECK, currentPost);
		personMoveInfo.setPostInfo(postInfoService.queryByPostId(postId));
		personMoveInfo.setStaffInfo(staffInfo);
		personMoveInfo.setPlanInfo(plan);
		personMoveInfo.setComments(comments);
		String[] deptNo=null;
		if(plan!=null){
			deptNo=plan.getPlanOrders().split(",");
		}
		if(personMoveInfo.getPostInfo() != null && personMoveInfo.getStaffInfo() != null && personMoveInfo.getPlanInfo() != null){
			personMoveInfo.setSubmitTime(DateUtil.toDate(DateUtil.getCurrentDate()));
			personMoveInfo.setMtoIds(mtoIds);
			if(deptNo.length!=0&&deptNo!=null){
				personMoveInfo.setStates(Integer.parseInt(deptNo[0]));
			}
			return infoDao.addPersonMoveInfo(personMoveInfo);
		}
		return 0;
	}

	@Override
	public int countTPages(int staffId, Integer states, int rows)
			throws Exception {
		personMoveInfo.setStaffInfo(staffInfoService.queryByStaffId(staffId));
		personMoveInfo.setStates(states);
		return infoDao.countTPages(personMoveInfo, rows);
	}

	@Override
	public PersonMoveInfoValue queryByMoveId(int moveId) throws Exception {
		personMoveInfo.setMoveId(moveId);
		return infoDao.queryByMoveId(personMoveInfo);
	}

	@Override
	public List<PersonMoveInfoValue> queryPersonMoveInfoWithPage(StaffInfoValue staffInfo,
			Integer states, int rows, int pageNo) throws Exception {
		personMoveInfo.setStaffInfo(staffInfo);
		personMoveInfo.setStates(states);
		return infoDao.queryPersonMoveInfoWithPage(personMoveInfo, rows, pageNo);
	}
	@Override
	public List<PersonMoveInfoValue> queryMoveCheck(StaffInfoValue staffInfo,
			Integer rows, Integer currpage)throws Exception {
		int sectionId = staffInfo.getArchiveInfo().getPostInfo().getSectionInfo().getSectionId();
        personMoveInfo.setStates(sectionId);
		return infoDao.queryPersonMoveInfoWithPage(personMoveInfo, rows, currpage);
	}
	@Override
	public List<PersonMoveInfoValue> queryPersonMoveInfoByCheckStaffId(
			int checkStaffId) throws Exception {
		personMoveCheck.setStaffInfo(staffInfoService.queryByStaffId(checkStaffId));
		if(personMoveCheck.getStaffInfo() != null){
			return infoDao.queryPersonMoveInfoByCheckStaffId(personMoveCheck);
		}
		return null;
	}
	
	public IPostInfoService getPostInfoService() {
		return postInfoService;
	}

	public void setPostInfoService(IPostInfoService postInfoService) {
		this.postInfoService = postInfoService;
	}

	public IStaffInfoService getStaffInfoService() {
		return staffInfoService;
	}

	public void setStaffInfoService(IStaffInfoService staffInfoService) {
		this.staffInfoService = staffInfoService;
	}

	public PersonMoveInfoValue getPersonMoveInfo() {
		return personMoveInfo;
	}

	public void setPersonMoveInfo(PersonMoveInfoValue personMoveInfo) {
		this.personMoveInfo = personMoveInfo;
	}

	public IPersonMoveInfoDao getInfoDao() {
		return infoDao;
	}

	public void setInfoDao(IPersonMoveInfoDao infoDao) {
		this.infoDao = infoDao;
	}

	public void setCheckPlanService(ICheckPlanService checkPlanService) {
		this.checkPlanService = checkPlanService;
	}

	public ICheckPlanService getCheckPlanService() {
		return checkPlanService;
	}
	
	public PersonMoveCheckValue getPersonMoveCheck() {
		return personMoveCheck;
	}

	public void setPersonMoveCheck(PersonMoveCheckValue personMoveCheck) {
		this.personMoveCheck = personMoveCheck;
	}

	@Override
	public void checkPass(StaffInfoValue staffInfo, Integer moveId,
			String comments) throws Exception{
		personMoveInfo.setMoveId(moveId); 
		PersonMoveInfoValue moveInfo = infoDao.queryByMoveId(personMoveInfo);
		String planOrders = moveInfo.getPlanInfo().getPlanOrders();

		int states = moveInfo.getStates();
		if(checkPlanService.getStates(planOrders, states)==IOrderStates.PASSED){
			String[] idWithName=moveInfo.getMtoIds().split(",");
			for(int i=0;i<idWithName.length;i++){
				String id=idWithName[i].split(":")[0];
				StaffInfoValue siv=staffInfoService.queryByStaffId(Integer.parseInt(id));
				siv.getArchiveInfo().setPostInfo(moveInfo.getPostInfo());
				archiveInfoService.modifyArchiveInfo(siv.getArchiveInfo());
				String messages ="调职记录：该人员已调职到"+siv.getArchiveInfo().getPostInfo().getSectionInfo().getSectionName()+"部门, 操作者："+staffInfo.getArchiveInfo().getStaffName();
				recordService.addRecordInfo(IRecordCategroyStates.MOVE, siv.getArchiveInfo().getArchiveId(), messages);
			}
			moveInfo.setStates(checkPlanService.getStates(planOrders, states));
			messageService.addMessageInfo("您于"+moveInfo.getStrSubmitTime()+"提交的人员调度申请的审核结果","您于"+moveInfo.getStrSubmitTime()+"提交的人员调度审请,通过审核。", moveInfo.getStaffInfo().getStaffId());
		}else{
			moveInfo.setStates(checkPlanService.getStates(planOrders, states));
		}
		infoDao.modifyPersonMoveInfo(moveInfo);
		personMoveCheck.setPersonMoveInfo(moveInfo);
		personMoveCheck.setStaffInfo(staffInfo);
		personMoveCheck.setComments(comments);
		personMoveCheck.setCheckTime(DateUtil.toDate(DateUtil
				.getCurrentDate()));
		personMoveCheck.setCheckOrder(states);
		personMoveCheck.setIsPassed(IGoodsInfoStates.CHECKPASSED);
		moveCheckService.addPersonMoveCheck(personMoveCheck);
	}

	@Override
	public void checkUnpass(StaffInfoValue staffInfo, Integer moveId,
			String comments) throws Exception{
		personMoveInfo.setMoveId(moveId); 
		PersonMoveInfoValue moveInfo = infoDao.queryByMoveId(personMoveInfo);
		personMoveCheck.setPersonMoveInfo(moveInfo);
		personMoveCheck.setStaffInfo(staffInfo);
		personMoveCheck.setComments(comments);
		personMoveCheck.setCheckTime(DateUtil.toDate(DateUtil
				.getCurrentDate()));
		personMoveCheck.setCheckOrder(moveInfo.getStates());
		personMoveCheck.setIsPassed(ICheckStates.CHECKFAILED);
		moveCheckService.addPersonMoveCheck(personMoveCheck);
		moveInfo.setStates(IOrderStates.FAILED);
		infoDao.modifyPersonMoveInfo(moveInfo);
		messageService.addMessageInfo("您于"+moveInfo.getStrSubmitTime()+"提交的人员调度申请的审核结果","您于"+moveInfo.getStrSubmitTime()+"提交的人员调度审请,未通过审核。", moveInfo.getStaffInfo().getStaffId());
	}

	public IPersonMoveCheckService getMoveCheckService() {
		return moveCheckService;
	}

	public void setMoveCheckService(IPersonMoveCheckService moveCheckService) {
		this.moveCheckService = moveCheckService;
	}

	public void setArchiveInfoService(IArchiveInfoService archiveInfoService) {
		this.archiveInfoService = archiveInfoService;
	}

	public IArchiveInfoService getArchiveInfoService() {
		return archiveInfoService;
	}

	public IRecordInfoService getRecordService() {
		return recordService;
	}

	public void setRecordService(IRecordInfoService recordService) {
		this.recordService = recordService;
	}

	public IMessageInfoService getMessageService() {
		return messageService;
	}

	public void setMessageService(IMessageInfoService messageService) {
		this.messageService = messageService;
	}
	
}
